package com.vent.share.service;

import com.alibaba.fastjson.JSON;
import com.vent.common.core.redis.RedisCache;
import com.vent.common.utils.DateUtils;
import com.vent.common.utils.StringUtils;
import com.vent.common.utils.http.HttpUtils;
import com.vent.mirai.constant.MiraiConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.contact.Friend;
import net.mamoe.mirai.event.events.FriendMessageEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description: 股票相关业务
 *
 * @author ZiXiu
 * @date 2021/8/20 11:26
 */
@Slf4j
@Component
public class ShareService {

    private RedisCache redisCache;

    public ShareService(RedisCache redisCache) {
        this.redisCache = redisCache;
    }

    public Map<String, List<Map<String, String>>> sendShareMakeNewList() {
        long expire = redisCache.expire(MiraiConstants.NEW_SHARE_LIST);
        List<Map<String, String>> list = new ArrayList<>();
        // redis中没有则从数据源网站获取
        if (expire == -2) {
            // 获取新股数据
            list = getShareDataFromSource();
            // 存入redis
            if (StringUtils.isNotEmpty(list)){
                redisCache.setCacheList(MiraiConstants.NEW_SHARE_LIST, list);
            }
        } else {
            list = redisCache.getCacheList(MiraiConstants.NEW_SHARE_LIST);
        }
        List<Map<String, String>> todayList = list.stream().filter(stringStringMap -> DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD).equals(stringStringMap.get("sub_date"))).collect(Collectors.toList());
        List<Map<String, String>> comingSoonList = list.stream().filter(stringStringMap -> !DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD).equals(stringStringMap.get("sub_date"))).collect(Collectors.toList());
        Collections.reverse(comingSoonList);
        Map<String, List<Map<String, String>>> resultListMap = new HashMap<>(2);
        resultListMap.put("todayList", StringUtils.isEmpty(todayList) ? new ArrayList<>() : todayList);
        resultListMap.put("comingSoonList", StringUtils.isEmpty(comingSoonList) ? new ArrayList<>() : comingSoonList);
        return resultListMap;
    }

    /**
     * 从数据源获取股票数据
     *
     * @return 有用的数据集合
     */
    public List<Map<String, String>> getShareDataFromSource() {
        List<Map<String, String>> list = new ArrayList<>();
        Map dataMap = JSON.parseObject(HttpUtils.sendPost("http://data.10jqka.com.cn/ipo/kzz/", null), Map.class);
        list = StringUtils.cast(dataMap.get("list"));
        // 取最新的100条
        if (list.size() > 100) {
            list = list.subList(0, 100);
        }
        // 过滤；只留今日和即将发行的新债
        list = list.stream().filter(stringStringMap -> {
            try {
                return DateUtils.parseDate(stringStringMap.get("sub_date"), DateUtils.YYYY_MM_DD).getTime() >= DateUtils.parseDate(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD), DateUtils.YYYY_MM_DD).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
                return false;
            }
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 开启提醒
     */
    public void openRemind(Long userId) {
        long expire = redisCache.expire(MiraiConstants.REMIND_USER_LIST);
        Set<Long> userIds = new HashSet<>();
        // redis中没有 则直接加
        if (expire == -2) {
            userIds.add(userId);
            // 存入redis
            redisCache.setCacheSet(MiraiConstants.REMIND_USER_LIST, userIds);
        } else {
            userIds = redisCache.getCacheSet(MiraiConstants.REMIND_USER_LIST);
            userIds.add(userId);
            redisCache.setCacheSet(MiraiConstants.REMIND_USER_LIST, userIds);
        }
    }

    /**
     * 关闭提醒
     */
    public void closeRemind(Long userId) {
        long expire = redisCache.expire(MiraiConstants.REMIND_USER_LIST);
        Set<Long> userIds = new HashSet<>();
        if (expire != -2) {
            userIds = redisCache.getCacheSet(MiraiConstants.REMIND_USER_LIST);
            userIds.remove(userId);
        }
        // 存入redis
        redisCache.deleteObject(MiraiConstants.REMIND_USER_LIST);
        redisCache.setCacheSet(MiraiConstants.REMIND_USER_LIST, userIds);
    }
}
